from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2 as cv
import numpy as np


@index_bp.get('/trans_pro')
@index_bp.post('/trans_pro')
@view_logging_required
@permission_required("trans:pro")
def trans_pro():
    if request.method == "GET":
        return render_template('admin/cv/trans.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            hor = request.form.get('width')
            ver = request.form.get('height')
            file_url = file_read(f)
            pic = cv.imread(file_url)
            res_pic = "static/download/trans.jpg"
            M = np.float32([[1, 0, ver], [0, 1, hor]])
            out_image = cv.warpAffine(pic, M, (pic.shape[1], pic.shape[0]))
            cv.imwrite(res_pic, out_image)
            return render_template('admin/cv/trans.html', pic=file_url, pic_res=res_pic)
        else:
            return "请传入正确的文件格式"